package com.gopay.bis.web.controller.ebank;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.common.util.TransUtils;
import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.domain.bank.bobj.ebank.BobjB2cMessage;
/**
 * @BobjB2cServerRes.java
 * @author LiLiLi
 */
@Controller
@Scope("prototype")
public class BobjB2cServerRes extends BankResBase {
	
	private static Log logger = LogFactory.getLog(BobjB2cServerRes.class);

    @RequestMapping(value = "/BobjB2cServerRes")
    protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
        logger.error("--------------- BobjB2cServerRes bankback ---------------");
        return super.execute(request, response);
    }
	
	@Override
	protected BobjB2cMessage buildMessage(HttpServletRequest request) {
		BobjB2cMessage message = new BobjB2cMessage();
		String bankSignData="";
		bankSignData = new String(request.getParameter("bankSignData"));
		message.setBank(BankConstants.BANK_LOGO_BOBJ);
		message.setSignMessage(bankSignData);
		message.setSpecial("1");
        //银行业务产品00个人网银支付
        message.setBankBusProd(BisBankBusinessProd._00);
        // 银行接口通道信息 00: 直连
        message.setBankIntrChann(BisBankInterfaceChannel._00);
		return message;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
		BobjB2cMessage message = (BobjB2cMessage)result.getModel("res");
		 OrderInfo orderInfo = new OrderInfo();
		 if(message!=null){
		 String retCode = message.getRetCode();
		 if(retCode!=null && retCode.equals("0000")){
	            orderInfo.setOrderId(message.getOrderNum());
	            orderInfo.setAmt(TransUtils.amountConvert2Yuan(message.getOrderAmt()));
	            orderInfo.setNotifyNum("1");
	            orderInfo.setPayStatus("00");
	            orderInfo.setRetCode("00");
	            orderInfo.setRetMsg(message.getErrMsg());
	        }else{
	            orderInfo.setOrderId(message.getOrderNum());
	            orderInfo.setAmt(TransUtils.amountConvert2Yuan(message.getOrderAmt()));
	            orderInfo.setNotifyNum("1");
	            orderInfo.setPayStatus("01");
	            orderInfo.setRetCode("01");
	            orderInfo.setRetMsg(message.getErrMsg());
	        }
		 }
		  orderInfo.setBankName("bobj");
		  orderInfo.setIsNotify(true);
	      return orderInfo;
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		 if(StringUtils.isEmpty(request.getParameter("bankSignData"))){
	        	return false;
	        }
		return true;
	}

}
